<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>获取 url 参数</title>
</head>
<body>


<script>
const paramGlo1 = 'http://www.nowcoder.com?key=1&key=2&key=3&test=4#hehe';
const paramGlo2 = 'key';
</script>

<script>
function getUrlParam1(sUrl, sKey) {
	let ret = []
	let obj = new URL(sUrl);
	var params = new URLSearchParams(obj.search);
	return params.getAll(sKey);
}

console.log(getUrlParam1(paramGlo1, paramGlo2));
</script>

<script>
function getUrlParam2(sUrl, sKey) {
	let ret = []
	let obj = new URL(sUrl);

	let searchUrl = obj.search.slice(1);
	let arr1 = searchUrl.split('&');

	arr1.forEach((item) => {
		let arrin = item.split('=');
		if(arrin[0] == sKey) {
			ret.push(arrin[1]);
		}
	})

	return ret;
}

console.log(getUrlParam2(paramGlo1, paramGlo2));
</script>

<script>
function getUrlParam3(sUrl, sKey) {
	let ret = {};
	let obj = new URL(sUrl);

	let searchUrl = obj.search.slice(1);
	let arr1 = searchUrl.split('&');

	arr1.forEach((item) => {
		let arrin = item.split('=');
		let [key, val] = arrin;

		
		if(ret[key] === void 0) {
			ret[key] = val;
		}else {
			ret[key] = [].concat(ret[key], val);
		}
	});

	return sKey === void 0 ? ret : (ret[sKey] || '');
}

console.log(getUrlParam3(paramGlo1, paramGlo2));
</script>
</body>
</html>